﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Vav.Data.QQ
{
    public class QQBase
    {
        private static string baseAddress = "https://api.q.qq.com/";
        #region 公共
        /// <summary>
        /// 获取基础access_token
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static async Task<AccessResponse> GetAccessToken(string appid, string appsecret)
        {
            using (var client = new HttpClient() { BaseAddress = new Uri(baseAddress) })
            {
                //var json = JsonConvert.SerializeObject(kindInfo);
                var response = await client.GetAsync($"api/getToken?grant_type=client_credential&appid={appid}&secret={appsecret}");
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<AccessResponse>(responseJson);
                return res;
            }
        }
        /// <summary>
        /// 校验access_token 是否过期
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public static async Task<bool> CheckAccessToken(string access_token)
        {
            using (var client = new HttpClient() { BaseAddress = new Uri(baseAddress) })
            {
                var flag = false;
                var response = await client.GetAsync($"cgi-bin/getcallbackip?access_token={access_token}");
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<GetcallbackipResponse>(responseJson);
                if (res.errcode == 0)
                {
                    flag = true;
                }
                return flag;
            }
        }
        #endregion


        #region 微信小程序
        /// <summary>
        /// 获取小程序用户信息 openid以及unionid
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <param name="js_code"></param>
        /// <returns></returns>
        public static async Task<ProUserUnionId> GetProUserUnionId(string appid, string appsecret, string js_code)
        {
            using (var client = new HttpClient() { BaseAddress = new Uri(baseAddress) })
            {
                var response = await client.GetAsync($"sns/jscode2session?appid={appid}&secret={appsecret}&js_code={js_code}&grant_type=authorization_code");
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<ProUserUnionId>(responseJson);
                return res;
            }
        }
        /// <summary>
        /// 二维码图片
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static async Task<Stream> GetProCode(GetWxCodeRequest getWxCodeRequest, string access_token)
        {
            using (var client = new HttpClient() { BaseAddress = new Uri(baseAddress) })
            {
                var json = JsonConvert.SerializeObject(getWxCodeRequest);
                var response = await client.PostAsync($"wxa/getwxacodeunlimit?access_token={access_token}", new StringContent(json, Encoding.UTF8, "application/json"));
                var stream = await response.Content.ReadAsStreamAsync();
                return stream;
            }
        }
        /// <summary>
        /// 小程序推送模板消息 
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static async Task<SendMsg> SendProTemplateMsg(JObject jObject, string access_token)
        {
            using (var client = new HttpClient() { BaseAddress = new Uri(baseAddress) })
            {
                var json = jObject.ToString();
                var response = await client.PostAsync($"cgi-bin/message/wxopen/template/send?access_token={access_token}", new StringContent(json, Encoding.UTF8, "application/json"));
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<SendMsg>(responseJson);
                return res;
            }
        }
        #endregion
    }
}
